package com.structure.algorithm;

/**
 * 返回数组中和为定值的两个数
 * Created by 乔磊 on 2015/11/18.
 */
public class TwoSum {
    private static int[] quickSort(){
        int[] s = {};
        return s;
    }
    private static String getTwoNum(int sum,int[] num){
        int lenth = num.length;
        int begin = 0;
        int end = lenth -1;
        int beginNum = 0;
        int endNum = 0;
        StringBuffer sb = new StringBuffer();
        while (begin < end) {
            beginNum = num[begin];
            endNum = num[end];
            //如果两个数的和值大于sum，end--
            if (beginNum + endNum > sum) {
                end--;
            }
            //如果两个数的和值小于sum，begin++
            if(beginNum + endNum < sum){
                begin++;
            }
            //如果两个数的和值等于sum，返回两个数
            if(beginNum + endNum ==sum){
                sb.append("beginNum is :").append(beginNum).append(",endNum is :").append(endNum);
                sb.append(",beginIndex is :").append(begin).append(",endIndex is :").append(end);
                break;
            }
        }
        return sb.toString();
    }
    public static void main(String[] args){
        int[] num = {1,2,3,4,5,5,6,7,8};
        int sum = 13;
        String res = getTwoNum(sum,num);
        System.out.print(res);
    }
}
